﻿#include "ctcpsocketclient.h"

TcpSocket::TcpSocket(QObject *parent)
    : QTcpSocket (parent)
{
    connect(this, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
}

TcpSocket::~TcpSocket()
{
    this->disconnectFromHost();
    if (this->state() != QAbstractSocket::UnconnectedState) {
        this->waitForDisconnected();
    }
}

void TcpSocket::connectTcpServer(const QString& ip, quint16 port)
{
    this->connectToHost(ip, port);
    //this->waitForConnected();
}

void TcpSocket::OnReadyRead()
{
    QByteArray reciverData = this->readAll();

    emit debugsocketmessage(this,QString(reciverData));
}

// 封包并发送
void TcpSocket::send(QString message)
{
    if(this->state() !=  QTcpSocket::ConnectedState || message.isEmpty())
    {
        return;
    }

    this->write(message.toUtf8());
}

